const express = require("express");
const cookieParser = require("cookie-parser");

const app = express();
app.use(cookieParser());

// 设置cookie
app.get("/set-cookie", (req, res) => {
  // res.cookie("name", "zhangsan"); // 会在浏览器关闭后，销毁
  res.cookie("name", "lisi", { maxAge: 60 * 1000 }); // 设置cookie时效，毫秒为单位
  res.cookie("theme", "blue");
  res.send("home");
});

// 删除cookie
app.get("/remove-cookie", (req, res) => {
  // 用例：用户退出登录时，清除用户登录的cookie
  res.clearCookie("name");
  res.send("删除成功");
});

// 获取cookie
app.get("/get-cookie", (req, res) => {
  console.log(req.cookies);
  res.send("欢迎您：" + req.cookies.name);
});

app.all("*", (req, res) => {
  res.send("not found");
});

app.listen(3000, () => {
  console.log("3000端口启动");
});
